package com.xinou.daily_report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.base.exception.BusException;
import com.xinou.common.util.BeanUtil;
import com.xinou.daily_report.PO.daily.DeductDateListPO;
import com.xinou.daily_report.PO.daily.DeductDatePO;
import com.xinou.daily_report.VO.daily.DeductDateVO;
import com.xinou.daily_report.entity.SysCalendar;
import com.xinou.daily_report.mapper.SysCalendarMapper;
import com.xinou.daily_report.service.SysCalendarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Month;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-03
 */
@Service
public class SysCalendarServiceImpl extends ServiceImpl<SysCalendarMapper, SysCalendar> implements SysCalendarService {

    @Override
    public ApiResponse<Void> addOrUpdateDeductDate(DeductDatePO po) {
        LocalDate deductDate = po.getSelectedDate();
        if(deductDate.isBefore(LocalDate.now())){
            throw new BusException("不可修改以前的日期");
        }
        if(deductDate==null){
            return new ApiResponse<>();
        }
        //如果日期存在，就取反，否则添加进去
        SysCalendar sysCalendar = getOne(new LambdaQueryWrapper<SysCalendar>().eq(SysCalendar::getSelectedDate, deductDate));
        if(sysCalendar!=null){
            sysCalendar.setDeductMoney(!sysCalendar.getDeductMoney());
            updateById(sysCalendar);
        }else{
            SysCalendar sysCalendar1 = BeanUtil.beanA2beanB(po, SysCalendar.class);
            save(sysCalendar1);
        }
        return new ApiResponse<>();
    }

    @Override
    public ApiResponse<DeductDateVO> deductDateList(DeductDateListPO po) {
        LocalDate today=po.getDate();
        LocalDate lastMonthDay=today.minusDays(45);
        LocalDate nextMonthDay=today.plusDays(45);
        List<SysCalendar> list = list(new LambdaQueryWrapper<SysCalendar>()
                .between(SysCalendar::getSelectedDate,lastMonthDay,nextMonthDay)
                .eq(SysCalendar::getDeductMoney, false));
        List<DeductDateVO> deductDateList = BeanUtil.listA2ListB(list, DeductDateVO.class);
        return new ApiResponse<>(deductDateList);
    }
}
